package com.gator.rabbitmq.pablo_rabbitmq.boot.rabbitmq.awesome.producer;

import com.gator.rabbitmq.pablo_rabbitmq.boot.rabbitmq.config.DelayQueueConfig;
import com.gator.rabbitmq.pablo_rabbitmq.boot.rabbitmq.config.PublishConfirmConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author PABLO
 * @Date 2022/6/5 23:29
 * @Desc 延迟队列生产者-延迟消息
 */
@Slf4j
@RestController
@RequestMapping("/ttl")
public class Provider {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    public void sendMsg(@RequestParam(name = "msg") String msg) {
        log.info("当前时间:{}-->sendMsgToQueue-->{}", new Date().toString(), msg);
        rabbitTemplate.convertAndSend("normal_exchange", "qa", "ttl10S-msg->" + msg);
        rabbitTemplate.convertAndSend("normal_exchange", "qb", "ttl30S-msg->" + msg);
    }

    @GetMapping("/sendTtl")
    public void sendTTLMsg(@RequestParam(name = "msg") String msg) {
        log.info("自定义TTL-->当前时间:{}-->sendMsgToQueue-->{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), msg);
        Long ttl = 20000l;
        rabbitTemplate.convertAndSend("normal_exchange", "qc", "ttl" + ttl + "HS-msg->" + msg, (message -> {
            //设置延时时间
            message.getMessageProperties().setExpiration(ttl.toString());
            return message;
        }));
    }

    /**
     * @Description: 基于插件的延迟队列发送消息
     * @Author: PABLO
     * @Date: 2022/6/6 15:59
     * @Params: [msg]
     * @Return: void
     **/
    @GetMapping("/sendByDelayed")
    public void sendByDelayed(@RequestParam(name = "msg") String msg) {
        log.info("当前时间:{}-->sendMsgToQueue-->{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), msg);
        Integer ttl = 20000;
        rabbitTemplate.convertAndSend(
                DelayQueueConfig.DELAY_EXCHANGE_NAME,
                DelayQueueConfig.DELAY_ROUTING_KEY,
                "ttl" + ttl + "HS-msg->" + msg,
                (message -> {
                    //设置延时时间
                    message.getMessageProperties().setDelay(ttl);
                    return message;
                }));
    }

    //测试发布确认机制 基于交换机
    @GetMapping("/sendMsgToExchangeAck")
    public void sendMsgToExchangeAck(@RequestParam(name = "msg") String msg) {
        log.info("测试交换机确认机制-->当前时间:{}-->sendMsgToQueue-->{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), msg);
        //真实场景一定是上游传来的某个唯一值
        CorrelationData correlationData = new CorrelationData("订单系统_order_id:HZHX85965825425425");
        rabbitTemplate.convertAndSend(
                PublishConfirmConfig.CONFIRM_EXCHANGE_NAME,
                PublishConfirmConfig.CONFIRM_ROUTING_KEY+"123",
                msg,
                correlationData);
    }
}
